/***** Lobxxx Translate Finished ******/
/*
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */

/*
 *
 *
 *
 *
 *
 * Written by Doug Lea with assistance from members of JCP JSR-166
 * Expert Group and released to the public domain, as explained at
 * http://creativecommons.org/publicdomain/zero/1.0/
 * <p>
 *  由Doug Lea在JCP JSR-166专家组成员的帮助下撰写,并发布到公共领域,如http://creativecommons.org/publicdomain/zero/1.0/
 * 
 */

package java.util.concurrent.locks;
import java.util.concurrent.TimeUnit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import sun.misc.Unsafe;

/**
 * Provides a framework for implementing blocking locks and related
 * synchronizers (semaphores, events, etc) that rely on
 * first-in-first-out (FIFO) wait queues.  This class is designed to
 * be a useful basis for most kinds of synchronizers that rely on a
 * single atomic {@code int} value to represent state. Subclasses
 * must define the protected methods that change this state, and which
 * define what that state means in terms of this object being acquired
 * or released.  Given these, the other methods in this class carry
 * out all queuing and blocking mechanics. Subclasses can maintain
 * other state fields, but only the atomically updated {@code int}
 * value manipulated using methods {@link #getState}, {@link
 * #setState} and {@link #compareAndSetState} is tracked with respect
 * to synchronization.
 *
 * <p>Subclasses should be defined as non-public internal helper
 * classes that are used to implement the synchronization properties
 * of their enclosing class.  Class
 * {@code AbstractQueuedSynchronizer} does not implement any
 * synchronization interface.  Instead it defines methods such as
 * {@link #acquireInterruptibly} that can be invoked as
 * appropriate by concrete locks and related synchronizers to
 * implement their public methods.
 *
 * <p>This class supports either or both a default <em>exclusive</em>
 * mode and a <em>shared</em> mode. When acquired in exclusive mode,
 * attempted acquires by other threads cannot succeed. Shared mode
 * acquires by multiple threads may (but need not) succeed. This class
 * does not &quot;understand&quot; these differences except in the
 * mechanical sense that when a shared mode acquire succeeds, the next
 * waiting thread (if one exists) must also determine whether it can
 * acquire as well. Threads waiting in the different modes share the
 * same FIFO queue. Usually, implementation subclasses support only
 * one of these modes, but both can come into play for example in a
 * {@link ReadWriteLock}. Subclasses that support only exclusive or
 * only shared modes need not define the methods supporting the unused mode.
 *
 * <p>This class defines a nested {@link ConditionObject} class that
 * can be used as a {@link Condition} implementation by subclasses
 * supporting exclusive mode for which method {@link
 * #isHeldExclusively} reports whether synchronization is exclusively
 * held with respect to the current thread, method {@link #release}
 * invoked with the current {@link #getState} value fully releases
 * this object, and {@link #acquire}, given this saved state value,
 * eventually restores this object to its previous acquired state.  No
 * {@code AbstractQueuedSynchronizer} method otherwise creates such a
 * condition, so if this constraint cannot be met, do not use it.  The
 * behavior of {@link ConditionObject} depends of course on the
 * semantics of its synchronizer implementation.
 *
 * <p>This class provides inspection, instrumentation, and monitoring
 * methods for the internal queue, as well as similar methods for
 * condition objects. These can be exported as desired into classes
 * using an {@code AbstractQueuedSynchronizer} for their
 * synchronization mechanics.
 *
 * <p>Serialization of this class stores only the underlying atomic
 * integer maintaining state, so deserialized objects have empty
 * thread queues. Typical subclasses requiring serializability will
 * define a {@code readObject} method that restores this to a known
 * initial state upon deserialization.
 *
 * <h3>Usage</h3>
 *
 * <p>To use this class as the basis of a synchronizer, redefine the
 * following methods, as applicable, by inspecting and/or modifying
 * the synchronization state using {@link #getState}, {@link
 * #setState} and/or {@link #compareAndSetState}:
 *
 * <ul>
 * <li> {@link #tryAcquire}
 * <li> {@link #tryRelease}
 * <li> {@link #tryAcquireShared}
 * <li> {@link #tryReleaseShared}
 * <li> {@link #isHeldExclusively}
 * </ul>
 *
 * Each of these methods by default throws {@link
 * UnsupportedOperationException}.  Implementations of these methods
 * must be internally thread-safe, and should in general be short and
 * not block. Defining these methods is the <em>only</em> supported
 * means of using this class. All other methods are declared
 * {@code final} because they cannot be independently varied.
 *
 * <p>You may also find the inherited methods from {@link
 * AbstractOwnableSynchronizer} useful to keep track of the thread
 * owning an exclusive synchronizer.  You are encouraged to use them
 * -- this enables monitoring and diagnostic tools to assist users in
 * determining which threads hold locks.
 *
 * <p>Even though this class is based on an internal FIFO queue, it
 * does not automatically enforce FIFO acquisition policies.  The core
 * of exclusive synchronization takes the form:
 *
 * <pre>
 * Acquire:
 *     while (!tryAcquire(arg)) {
 *        <em>enqueue thread if it is not already queued</em>;
 *        <em>possibly block current thread</em>;
 *     }
 *
 * Release:
 *     if (tryRelease(arg))
 *        <em>unblock the first queued thread</em>;
 * </pre>
 *
 * (Shared mode is similar but may involve cascading signals.)
 *
 * <p id="barging">Because checks in acquire are invoked before
 * enqueuing, a newly acquiring thread may <em>barge</em> ahead of
 * others that are blocked and queued.  However, you can, if desired,
 * define {@code tryAcquire} and/or {@code tryAcquireShared} to
 * disable barging by internally invoking one or more of the inspection
 * methods, thereby providing a <em>fair</em> FIFO acquisition order.
 * In particular, most fair synchronizers can define {@code tryAcquire}
 * to return {@code false} if {@link #hasQueuedPredecessors} (a method
 * specifically designed to be used by fair synchronizers) returns
 * {@code true}.  Other variations are possible.
 *
 * <p>Throughput and scalability are generally highest for the
 * default barging (also known as <em>greedy</em>,
 * <em>renouncement</em>, and <em>convoy-avoidance</em>) strategy.
 * While this is not guaranteed to be fair or starvation-free, earlier
 * queued threads are allowed to recontend before later queued
 * threads, and each recontention has an unbiased chance to succeed
 * against incoming threads.  Also, while acquires do not
 * &quot;spin&quot; in the usual sense, they may perform multiple
 * invocations of {@code tryAcquire} interspersed with other
 * computations before blocking.  This gives most of the benefits of
 * spins when exclusive synchronization is only briefly held, without
 * most of the liabilities when it isn't. If so desired, you can
 * augment this by preceding calls to acquire methods with
 * "fast-path" checks, possibly prechecking {@link #hasContended}
 * and/or {@link #hasQueuedThreads} to only do so if the synchronizer
 * is likely not to be contended.
 *
 * <p>This class provides an efficient and scalable basis for
 * synchronization in part by specializing its range of use to
 * synchronizers that can rely on {@code int} state, acquire, and
 * release parameters, and an internal FIFO wait queue. When this does
 * not suffice, you can build synchronizers from a lower level using
 * {@link java.util.concurrent.atomic atomic} classes, your own custom
 * {@link java.util.Queue} classes, and {@link LockSupport} blocking
 * support.
 *
 * <h3>Usage Examples</h3>
 *
 * <p>Here is a non-reentrant mutual exclusion lock class that uses
 * the value zero to represent the unlocked state, and one to
 * represent the locked state. While a non-reentrant lock
 * does not strictly require recording of the current owner
 * thread, this class does so anyway to make usage easier to monitor.
 * It also supports conditions and exposes
 * one of the instrumentation methods:
 *
 *  <pre> {@code
 * class Mutex implements Lock, java.io.Serializable {
 *
 *   // Our internal helper class
 *   private static class Sync extends AbstractQueuedSynchronizer {
 *     // Reports whether in locked state
 *     protected boolean isHeldExclusively() {
 *       return getState() == 1;
 *     }
 *
 *     // Acquires the lock if state is zero
 *     public boolean tryAcquire(int acquires) {
 *       assert acquires == 1; // Otherwise unused
 *       if (compareAndSetState(0, 1)) {
 *         setExclusiveOwnerThread(Thread.currentThread());
 *         return true;
 *       }
 *       return false;
 *     }
 *
 *     // Releases the lock by setting state to zero
 *     protected boolean tryRelease(int releases) {
 *       assert releases == 1; // Otherwise unused
 *       if (getState() == 0) throw new IllegalMonitorStateException();
 *       setExclusiveOwnerThread(null);
 *       setState(0);
 *       return true;
 *     }
 *
 *     // Provides a Condition
 *     Condition newCondition() { return new ConditionObject(); }
 *
 *     // Deserializes properly
 *     private void readObject(ObjectInputStream s)
 *         throws IOException, ClassNotFoundException {
 *       s.defaultReadObject();
 *       setState(0); // reset to unlocked state
 *     }
 *   }
 *
 *   // The sync object does all the hard work. We just forward to it.
 *   private final Sync sync = new Sync();
 *
 *   public void lock()                { sync.acquire(1); }
 *   public boolean tryLock()          { return sync.tryAcquire(1); }
 *   public void unlock()              { sync.release(1); }
 *   public Condition newCondition()   { return sync.newCondition(); }
 *   public boolean isLocked()         { return sync.isHeldExclusively(); }
 *   public boolean hasQueuedThreads() { return sync.hasQueuedThreads(); }
 *   public void lockInterruptibly() throws InterruptedException {
 *     sync.acquireInterruptibly(1);
 *   }
 *   public boolean tryLock(long timeout, TimeUnit unit)
 *       throws InterruptedException {
 *     return sync.tryAcquireNanos(1, unit.toNanos(timeout));
 *   }
 * }}</pre>
 *
 * <p>Here is a latch class that is like a
 * {@link java.util.concurrent.CountDownLatch CountDownLatch}
 * except that it only requires a single {@code signal} to
 * fire. Because a latch is non-exclusive, it uses the {@code shared}
 * acquire and release methods.
 *
 *  <pre> {@code
 * class BooleanLatch {
 *
 *   private static class Sync extends AbstractQueuedSynchronizer {
 *     boolean isSignalled() { return getState() != 0; }
 *
 *     protected int tryAcquireShared(int ignore) {
 *       return isSignalled() ? 1 : -1;
 *     }
 *
 *     protected boolean tryReleaseShared(int ignore) {
 *       setState(1);
 *       return true;
 *     }
 *   }
 *
 *   private final Sync sync = new Sync();
 *   public boolean isSignalled() { return sync.isSignalled(); }
 *   public void signal()         { sync.releaseShared(1); }
 *   public void await() throws InterruptedException {
 *     sync.acquireSharedInterruptibly(1);
 *   }
 * }}</pre>
 *
 * <p>
 *  提供用于实现依赖先进先出(FIFO)等待队列的阻塞锁和相关同步器(信号量,事件等)的框架。这个类被设计为大多数依赖于单个原子{@code int}值来表示状态的同步器的有用基础。
 * 子类必须定义更改此状态的受保护方法,并定义该状态对于正在获取或释放的对象的含义。鉴于这些,这个类中的其他方法执行所有的排队和阻塞机制。
 * 子类可以维护其他状态字段,但只会跟踪使用方法{@link #getState},{@link #setState}和{@link #compareAndSetState}处理的原子级更新的{@code int}
 * 值以进行同步。
 * 子类必须定义更改此状态的受保护方法,并定义该状态对于正在获取或释放的对象的含义。鉴于这些,这个类中的其他方法执行所有的排队和阻塞机制。
 * 
 *  <p>子类应定义为非公共内部辅助类,用于实现其封闭类的同步属性。类{@code AbstractQueuedSynchronizer}不实现任何同步接口。
 * 相反,它定义了诸如{@link #acquireInterruptibly}之类的方法,可以通过具体锁和相关同步器来适当地调用以实现它们的公共方法。
 * 
 * <p>此类别支持默认的<em>独占</em>模式和<em>共享</em>模式。在独占模式下获取时,其他线程尝试获取无法成功。共享模式由多个线程获取可能(但不需要)成功。
 * 这个类不"理解"这些差异除了在机械意义上,当共享模式获取成功时,下一个等待线程(如果存在)还必须确定它是否可以获取。在不同模式下等待的线程共享同一个FIFO队列。
 * 通常,实现子类只支持这些模式之一,但两者都可以在例如{@link ReadWriteLock}中发挥作用。仅支持独占或仅共享模式的子类不需要定义支持未使用模式的方法。
 * 
 * <p>此类定义了一个可以用作{@link Condition}实现的嵌套{@link ConditionObject}类,支持独占模式的子类{@link }报告
 * 同步是否独占当前线程,使用当前{@link #getState}值调用的方法{@link #release}完全释放此对象,并且给定此保存的状态值,{@link #acquire}最终将此对象恢复到其先
 * 前获取的状态。
 * 否则,{@code AbstractQueuedSynchronizer}方法将创建此类条件,因此如果无法满足此约束,请不要使用它。
 *  {@link ConditionObject}的行为当然取决于它的同步器实现的语义。
 * 
 *  <p>此类提供内部队列的检查,检测和监视方法,以及条件对象的类似方法。这些可以使用{@code AbstractQueuedSynchronizer}的同步机制根据需要导出到类中。
 * 
 *  <p>此类的序列化仅存储底层原子整数维护状态,因此反序列化对象具有空线程队列。要求可序列化的典型子类将定义一个{@code readObject}方法,用于在反序列化后将其恢复为已知的初始状态。
 * 
 *  <h3>用法</h3>
 * 
 * <p>要使用此类作为同步器的基础,请通过使用{@link #getState},{@link #setState}和/或{@}来检查和/或修改同步状态,重新定义以下方法link #compareAndS
 * etState}：。
 * 
 * <ul>
 *  <li> {@link #tryAcquire} <li> {@link #tryRelease} <li> {@link #tryAcquireShared} <li> {@link #tryReleaseShared}
 *  <li> {@link #isHeldExclusively}。
 * </ul>
 * 
 *  默认情况下,这些方法中的每一个都抛出{@link UnsupportedOperationException}。这些方法的实现必须是内部线程安全的,并且通常应该是短的而不是阻塞的。
 * 定义这些方法是<em>仅</em>支持的使用此类的方法。所有其他方法都声明为{@code final},因为它们不能独立变化。
 * 
 *  <p>您还可以从{@link AbstractOwnableSynchronizer}中找到继承的方法,用于跟踪拥有独占同步器的线程。
 * 鼓励您使用它们 - 这使得监视和诊断工具能够帮助用户确定哪些线程持有锁。
 * 
 *  <p>即使此类是基于内部FIFO队列,它不会自动强制执行FIFO采集策略。独占同步的核心形式如下：
 * 
 * <pre>
 *  获取：while(！tryAcquire(arg)){如果线程尚未排队,则将其排队</em>; <em>可能阻止当前线程</em>; }}
 * 
 *  发布：if(tryRelease(arg))<em>取消屏蔽第一个排队的主题</em>;
 * </pre>
 * 
 *  (共享模式类似,但可能涉及级联信号。)
 * 
 * <p id ="barging">因为获取中的检查在入队之前被调用,新获取的线程可以在被阻塞和排队的其他线程之前<em>插入</em>。
 * 但是,如果需要,您可以通过内部调用一个或多个检查方法来定义{@code tryAcquire}和/或{@code tryAcquireShared}以禁用驳船,从而提供<em>公平</em> FIFO获
 * 取顺序。
 * <p id ="barging">因为获取中的检查在入队之前被调用,新获取的线程可以在被阻塞和排队的其他线程之前<em>插入</em>。
 * 特别是,如果{@link #hasQueuedPredecessors}(专门设计为由公平同步器使用的方法)返回{@code true},大多数公平同步器可以定义{@code tryAcquire}返回
 * {@code false}。
 * <p id ="barging">因为获取中的检查在入队之前被调用,新获取的线程可以在被阻塞和排队的其他线程之前<em>插入</em>。其他变化是可能的。
 * 
 * <p>对于默认投放(也称为<em>贪婪</em>,<em>放弃</em>和<em>运输避免</em>)策略,吞吐量和可扩展性通常最高。
 * 虽然这不能保证是公平的或无饥饿的,早期的排队线程被允许在以后排队的线程之前重新组合,并且每个重新聚合具有成功抵抗进入线程的无偏见的机会。
 * 此外,虽然获取不"旋转"在通常意义上,它们可以在阻塞之前执行{@code tryAcquire}的多次调用与其他计算。这提供了旋转的大多数好处,当独占同步只是简单地举行,没有大多数负债,而不是。
 * 如果需要,您可以通过前面的调用来获取具有"快速路径"检查的方法(可能预先检查{@link #hasContended}和/或{@link #hasQueuedThreads}),如果同步器可能不是争辩。
 * 此外,虽然获取不"旋转"在通常意义上,它们可以在阻塞之前执行{@code tryAcquire}的多次调用与其他计算。这提供了旋转的大多数好处,当独占同步只是简单地举行,没有大多数负债,而不是。
 * 
 *  <p>此类为同步提供了一个有效和可扩展的基础,部分是通过将其使用范围专门用于可依赖于{@code int}状态,获取和释放参数的同步器以及内部FIFO等待队列。
 * 如果这不足够,您可以使用{@link java.util.concurrent.atomic atomic}类,您自己的自定义{@link java.util.Queue}类和{@link LockSupport}
 * 阻止支持。
 *  <p>此类为同步提供了一个有效和可扩展的基础,部分是通过将其使用范围专门用于可依赖于{@code int}状态,获取和释放参数的同步器以及内部FIFO等待队列。
 * 
 *  <h3>使用示例</h3>
 * 
 * <p>这里是一个非重入互斥锁类,它使用值零表示解锁状态,一个表示锁定状态。虽然不可重入锁并不严格要求记录当前所有者线程,但是这个类仍然这样做,使得更容易监视使用。它还支持条件并暴露一种仪器方法：
 * 
 *  <pre> {@code类Mutex实现Lock,java.io.Serializable {
 * 
 *  //我们的内部辅助类private static class Sync extends AbstractQueuedSynchronizer {//报告是否处于锁定状态protected boolean isHeldExclusively(){return getState()== 1; }
 * }。
 * 
 *  //获取锁如果状态为零public boolean tryAcquire(int acquire){assert acquire == 1; //否则未使用if(compareAndSetState(0,1)){setExclusiveOwnerThread(Thread.currentThread()); return true; }
 *  return false; }}。
 * 
 *  //通过将状态设置为零来释放锁保护boolean tryRelease(int releases){assert releases == 1; //否则未使用if(getState()== 0)throw new IllegalMonitorStateException(); setExclusiveOwnerThread(null); setState(0); return true; }
 * }。
 * 
 *  //提供条件条件newCondition(){return new ConditionObject(); }}
 * 
 *  //反序列化private void readObject(ObjectInputStream s)throws IOException,ClassNotFoundException {s.defaultReadObject(); setState(0); // reset to unlocked state}
 * }。
 * 
 * //同步对象做所有的辛勤工作。我们只是向前走。 private final Sync sync = new Sync();
 * 
 *  public void lock(){sync.acquire(1); } public boolean tryLock(){return sync.tryAcquire(1); } public v
 * oid unlock(){sync.release(1); } public Condition newCondition(){return sync.newCondition(); } public 
 * boolean isLocked(){return sync.isHeldExclusively(); } public boolean hasQueuedThreads(){return sync.hasQueuedThreads(); }
 *  public void lockInterruptibly()throws InterruptedException {sync.acquireInterruptably(1); } public b
 * oolean tryLock(long timeout,TimeUnit unit)throws InterruptedException {return sync.tryAcquireNanos(1,unit.toNanos(timeout)); }
 * }} </pre>。
 * 
 *  <p>这里是一个类似于{@link java.util.concurrent.CountDownLatch CountDownLatch}的锁类,只不过它只需要一个{@code signal}即可。
 * 因为锁存器是非排他的,它使用{@code shared}获取和释放方法。
 * 
 *  <pre> {@code class BooleanLatch {
 * 
 *  私有静态类同步扩展AbstractQueuedSynchronizer {boolean isSignalled(){return getState()！= 0; }}
 * 
 *  protected int tryAcquireShared(int ignore){return isSignalled()? 1：-1; }}
 * 
 *  protected boolean tryReleaseShared(int ignore){setState(1); return true; }}
 * 
 * private final Sync sync = new Sync(); public boolean isSignalled(){return sync.isSignalled(); } publi
 * c void signal(){sync.releaseShared(1); } public void await()throws InterruptedException {sync.acquireSharedInterruptably(1); }
 * }} </pre>。
 * 
 * 
 * @since 1.5
 * @author Doug Lea
 */
public abstract class AbstractQueuedSynchronizer
    extends AbstractOwnableSynchronizer
    implements java.io.Serializable {

    private static final long serialVersionUID = 7373984972572414691L;

    /**
     * Creates a new {@code AbstractQueuedSynchronizer} instance
     * with initial synchronization state of zero.
     * <p>
     *  创建一个初始同步状态为零的新{@code AbstractQueuedSynchronizer}实例。
     * 
     */
    protected AbstractQueuedSynchronizer() { }

    /**
     * <p>
     *  等待队列节点类。
     * 
     *  <p>等待队列是"CLH"(Craig,Landin和Hagersten)锁队列的变体。 CLH锁通常用于自旋锁。
     * 我们使用它们来阻塞同步器,但是使用相同的基本策略来保存关于其节点的前导中的线程的一些控制信息。
     * 每个节点中的"状态"字段跟踪线程是否应该阻塞。节点在其前任释放时发出信号。
     * 否则队列的每个节点用作保持单个等待线程的特定通知类型的监视器。状态字段不控制线程是否授予锁等。
     * 线程可以尝试获取它是否在队列中的第一个。但是首先不能保证成功;它只给予权利来抗衡。
     * 所以当前发布的竞争者线程可能需要等待。
     * 
     *  <p>要加入到CLH锁中,您将其作为新尾部进行原子拼接。要出列,您只需设置head字段。
     * <pre>
     *  + ------ + prev + ----- + + ----- + head | | <---- | | <---- | |尾+ ------ + + ----- + + ----- +
     * </pre>
     * 
     * <p>插入到CLH队列中只需要对"tail"执行单个原子操作,因此有一个从未排队到排队的简单原子分界点。类似地,出列仅涉及更新"头部"。
     * 然而,节点需要更多的工作来确定他们的后继者是谁,部分地处理由于超时和中断而可能的取消。
     * 
     *  <p>"prev"链接(不在原始CLH锁中使用)主要用于处理取消。如果节点被取消,其后继者(通常)重新链接到未取消的前导。
     * 对于在自旋锁的情况下类似的力学的解释,参见Scott和Scherer在http://www.cs.rochester.edu/u/scott/synchronization/的论文。
     * 
     *  <p>我们还使用"下一个"链接来实现阻止力学。每个节点的线程ID保存在其自己的节点中,因此前趋信号通知下一个节点通过遍历下一个链路来确定哪个线程。
     * 后继者的确定必须避免与新排队节点的比赛,以设置其前辈的"下一个"字段。当必要时,当节点的后继者看起来为空时,通过从原子更新的"尾部"向后检查来解决这个问题。
     *  (或者,换句话说,下一链接是优化,使得我们通常不需要反向扫描。)。
     * 
     * <p>取消向基本算法引入了一些保守性。由于我们必须轮询其他节点的取消,所以我们可能会错过注意一个被取消的节点是否在我们之前或之后。
     * 这是由取消时总是不公平的继任者处理的,允许他们稳定在一个新的前辈,除非我们能找到一个未履行的前任谁将承担这个责任。
     * 
     *  <p> CLH队列需要一个虚标头节点才能开始。但是我们不会在建设上创建它们,因为如果没有争用,那么这是浪费的努力。相反,构造节点,并且在第一争用时设置头和尾指针。
     * 
     *  <p>等待的线程条件使用相同的节点,但使用额外的链接。条件仅需要链接简单(非并发)链接队列中的节点,因为它们仅在被独占保持时才被访问。等待时,将节点插入条件队列中。根据信号,节点被传送到主队列。
     * 状态字段的特殊值用于标记节点所在的队列。
     * 
     *  <p>感谢Dave Dice,Mark Moir,Victor Luchangco,Bill Scherer和Michael Scott,以及JSR-166专家组的成员,对这个课程的设计提出了有益的想
     * 法,讨论和批评。
     * 
     */
    static final class Node {
        /** Marker to indicate a node is waiting in shared mode */
        static final Node SHARED = new Node();
        /** Marker to indicate a node is waiting in exclusive mode */
        static final Node EXCLUSIVE = null;

        /** waitStatus value to indicate thread has cancelled */
        static final int CANCELLED =  1;
        /** waitStatus value to indicate successor's thread needs unparking */
        static final int SIGNAL    = -1;
        /** waitStatus value to indicate thread is waiting on condition */
        static final int CONDITION = -2;
        /**
         *  waitStatus值指示下一个acquireShared应无条件传播
         * 
         */
        static final int PROPAGATE = -3;

        /**
         * 状态字段,只接受值：
         * SIGNAL：此节点的后继节点(或将很快)被阻塞(通过公园),因此当它释放或取消时,当前节点必须取消其后继。
         *          为了避免竞争,获取方法必须首先指示他们需要一个信号,然后重试原子获取,然后,在失败时,阻止。
         * CANCELED：由于超时或中断,此节点被取消。节点永远不会离开这个状态。
         *           特别地,具有被取消节点的线程从不再次阻塞。
         * CONDITION：此节点当前处于条件队列。它不会被用作同步队列节点,直到被传送,此时状态将被设置为0.(使用此值与字段的其他用法无关,但简化了机制。)
         *
         * PROPAGATE：A releaseShared应该传播到其他节点。这在doReleaseShared中设置(仅用于头节点),以确保传播继续,即使其他操作已干预。
         *
         * 0：上面没有。
         * 
         *  这些值以数字排列以简化使用。非负值意味着节点不需要信号。所以,大多数代码不需要检查特定的值,只是为了。
         * 
         *  该字段对于正常同步节点初始化为0,对于条件节点初始化为CONDITION。它使用CAS(或在可能的情况下,无条件的易失性写入)修改。
         * 
         */
        volatile int waitStatus;

        /**
         * 链接到前任节点,当前节点/线程依赖它来检查waitStatus。在入队期间分配,并且只有在出队时才归零(为了GC的目的)。
         * 此外,在取消前任者时,我们在找到未取消的一个时进行短路,这将始终存在,因为头节点从未被取消：
         * 节点仅作为成功获取的结果而变为头。被取消的线程永远不会成功获取,线程只取消自身,而不是任何其他节点。
         * 
         */
        volatile Node prev;

        /**
         *  链接到后续节点,当前节点/线程释放时释放。在入队期间分配,在绕过取消的前趋时调整,并在出列时取消(为了GC的目的)。
         *  enq操作不会分配前导的next字段,直到附加之后,因此看到null下一个字段不一定意味着该节点在队列的末尾。
         *  但是,如果下一个字段显示为空,我们可以从尾部扫描prev,仔细检查。
         * 被取消的节点的下一个字段被设置为指向节点本身而不是null,以使isOnSyncQueue的生活更容易。
         * 
         */
        volatile Node next;

        /**

         *  将此节点入列的线程。初始化建设,使用后清零。
         * 
         */
        volatile Thread thread;

        /**
         * 链接到下一个节点等待条件,或特殊值SHARED。因为条件队列只有在保持在独占模式时才被访问,
         * 所以我们只需要一个简单的链接队列来保存节点,同时等待条件。然后将它们转移到队列中以重新获取。
         * 并且因为条件只能是排他的,我们通过使用特殊的值来指示共享模式来保存一个字段。
         * 
         */
        Node nextWaiter;

        /**
         *  如果节点在共享模式下等待,则返回true。
         * 
         */
        final boolean isShared() {
            return nextWaiter == SHARED;
        }

        /**
         *  返回上一个节点,如果为null,则抛出NullPointerException。当前导不能为null时使用。空检查可以省略,但是存在以帮助VM。
         */
        final Node predecessor() throws NullPointerException {
            Node p = prev;
            if (p == null)
                throw new NullPointerException();
            else
                return p;
        }

        Node() {    // Used to establish initial head or SHARED marker
        }

        Node(Thread thread, Node mode) {     // Used by addWaiter
            this.nextWaiter = mode;
            this.thread = thread;
        }

        Node(Thread thread, int waitStatus) { // Used by Condition
            this.waitStatus = waitStatus;
            this.thread = thread;
        }
    }

    /**
     *  头结点,懒加载。除了初始化,它只通过方法setHead修改。注意：如果头存在,它的waitStatus被保证不被取消。
     * 
     */
    private transient volatile Node head;

    /**
     *  尾的等待队列,延迟初始化。仅通过方法enq修改以添加新的等待节点。
     */
    private transient volatile Node tail;

    /**
     *  同步状态。
     */
    private volatile int state;

    /**
     *  返回同步状态的当前值。此操作具有{@code volatile}读取的内存语义。
     */
    protected final int getState() {
        return state;
    }

    /**
     *  设置同步状态的值。此操作具有{@code volatile}写入的存储器语义。
     */
    protected final void setState(int newState) {
        state = newState;
    }

    /**
     *  如果当前状态值等于预期值,则将同步状态原子地设置为给定的更新值。此操作具有{@code volatile}读取和写入的存储器语义。
     */
    //CAS设定state的值
    protected final boolean compareAndSetState(int expect, int update) {
        // See below for intrinsics setup to support this
        return unsafe.compareAndSwapInt(this, stateOffset, expect, update);
    }


    /**
     * 纳秒的数量,它旋转更快,而不是使用定时公园。粗略估计足以用非常短的超时提高响应性。
     */
    static final long spinForTimeoutThreshold = 1000L;

    /**
     *  将节点插入队列,必要时进行初始化。见上图。
     */
    //采用CAS将当前线程节点插入队列中
    private Node enq(final Node node) {
        for (;;) {
            Node t = tail;//获取队尾
            if (t == null) { // Must initialize
                //队列为空时初始化队列
                if (compareAndSetHead(new Node()))
                    tail = head;
            } else {
                node.prev = t;
                if (compareAndSetTail(t, node)) {
                    t.next = node;
                    return t;
                }
            }
        }
    }

    /**
     *  创建和排队当前线程和给定模式的节点。
     */
    //将获取锁失败的线程加入队列中
    private Node addWaiter(Node mode) {
        //创建一个新的节点,封装有当前的线程实例
        Node node = new Node(Thread.currentThread(), mode);
        // Try the fast path of enq; backup to full enq on failure
        Node pred = tail;//队尾节点
        if (pred != null) {
            node.prev = pred;
            //采用CAS将当前节点设为队尾节点
            if (compareAndSetTail(pred, node)) {
                pred.next = node;
                return node;
            }
        }
        enq(node);
        return node;
    }

    /**
     *  将队列头设置为节点,从而出队。仅由获取方法调用。也为null为了GC和为了抑制不必要的信号和遍历的未使用的字段。
     */
    private void setHead(Node node) {
        head = node;
        node.thread = null;
        node.prev = null;
    }

    /**
     *  唤醒节点的后继(如果存在)。
     */
    private void unparkSuccessor(Node node) {
        //如果状态为负(即,可能需要信号)尝试在预期的信令中清除。如果这失败或者如果状态通过等待线程而改变,则是OK。
        int ws = node.waitStatus;
        if (ws < 0)
            compareAndSetWaitStatus(node, ws, 0);

        Node s = node.next;
        //如果下一个节点不存在或者已经取消等待,那么从尾节点向前遍历,找到离当前节点最近的而且在等待中的节点
        //疑问:为什么从队尾遍历不从当前节点开始遍历?
        //答:因为当前节点的下一个节点可能为空,往下遍历不了
        if (s == null || s.waitStatus > 0) {
            s = null;
            for (Node t = tail; t != null && t != node; t = t.prev)
                if (t.waitStatus <= 0)
                    s = t;
        }

        //唤醒离当前节点最近的且在等待中的节点
        if (s != null)
            LockSupport.unpark(s.thread);
    }

    /**
     *  释放共享模式的操作 - 信号后继并确保传播。 (注意：对于独占模式,释放只是等于调用unparkSuccessor的head,如果它需要信号)。
     * 
     */
    private void doReleaseShared() {
        /*
         * 确保释放传播,即使有其他进行中的获取/释放。如果它需要信号,则以通常的方式尝试解除头的处理器。但如果不是,状态设置为PROPAGATE以确保在释放后,传播继续。
         * 另外,我们必须循环,以便在我们这样做的时候添加一个新的节点。此外,与unparkSuccessor的其他用法不同,我们需要知道CAS是否重置状态失败,如果这样重新检查。
         * 
         */
        for (;;) {
            Node h = head;
            if (h != null && h != tail) {
                int ws = h.waitStatus;
                if (ws == Node.SIGNAL) {
                    if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))
                        continue;            // loop to recheck cases
                    //从头节点开始唤醒后续线程
                    unparkSuccessor(h);
                }
                else if (ws == 0 &&
                         !compareAndSetWaitStatus(h, 0, Node.PROPAGATE))
                    continue;                // loop on failed CAS
            }
            if (h == head)                   // loop if head changed
                break;
        }
    }

    /**
     *  设置队列头,并检查后继者是否可能在共享模式下等待,如果传播> 0或PROPAGATE状态,则传播。
     */
    private void setHeadAndPropagate(Node node, int propagate) {
        Node h = head; // Record old head for check below
        setHead(node);
        /*
         *  尝试向下一个排队节点发出信号if：传播由呼叫者指示,或者由前一个操作记录(作为在setHead之前或之后的h.waitStatus)(注意：这使用waitStatus的符号检查,因为PROPAGATE
         * 状态可能转换为SIGNAL。
         *  )和下一个节点正在等待共享模式,或者我们不知道,因为它显示为null。
         *  这些检查中的保守性可能导致不必要的唤醒,但是只有当有多个赛车获取/释放时,所以大多数现在或很快需要信号。
         */
        if (propagate > 0 || h == null || h.waitStatus < 0 ||
            (h = head) == null || h.waitStatus < 0) {
            Node s = node.next;
            if (s == null || s.isShared())
                doReleaseShared();
        }
    }


    /**
     *  取消正在进行的尝试获取。
     */
    private void cancelAcquire(Node node) {
        if (node == null)
            return;

        node.thread = null;

        Node pred = node.prev;
        while (pred.waitStatus > 0)
            node.prev = pred = pred.prev;

        Node predNext = pred.next;

        node.waitStatus = Node.CANCELLED;

        // If we are the tail, remove ourselves.
        if (node == tail && compareAndSetTail(node, pred)) {
            compareAndSetNext(pred, predNext, null);
        } else {
            int ws;
            if (pred != head &&
                ((ws = pred.waitStatus) == Node.SIGNAL ||
                 (ws <= 0 && compareAndSetWaitStatus(pred, ws, Node.SIGNAL))) &&
                pred.thread != null) {
                Node next = node.next;
                if (next != null && next.waitStatus <= 0)
                    compareAndSetNext(pred, predNext, next);
            } else {
                unparkSuccessor(node);
            }

            node.next = node; // help GC
        }
    }

    /**
     *  检查和更新未能获取的节点的状态。如果线程应阻塞,则返回true。这是所有采集环路中的主要信号控制。需要pred == node.prev。
     */
    private static boolean shouldParkAfterFailedAcquire(Node pred, Node node) {
        int ws = pred.waitStatus;//获取前置线程的等待状态
        if (ws == Node.SIGNAL)
            /*
             *  此节点已设置状态,要求发布信号,以便它可以安全地停放。
             * 
             */
            return true;
        if (ws > 0) {
            /*
             * 前导已取消。跳过前辈并指示重试。
             */
            do {
                //往队首遍历, 将node的前置节点指向最近的一个waitStatus<=0的节点(此处更改了node的前置节点)
                node.prev = pred = pred.prev;
            } while (pred.waitStatus > 0);
            pred.next = node;
        } else {
            /*
             *  waitStatus必须为0或PROPAGATE。表示我们需要一个信号,但不停泊。来电者需要重试,以确保在停车之前无法获取。
             */
            compareAndSetWaitStatus(pred, ws, Node.SIGNAL);
        }
        return false;
    }

    /**
     *  方便中断当前线程。
     * 
     */
    static void selfInterrupt() {
        Thread.currentThread().interrupt();
    }

    /**
     * Convenience method to park and then check if interrupted
     *
     * <p>
     *  方便的方法停车,然后检查是否中断
     * 
     * 
     * @return {@code true} if interrupted
     */
    private final boolean parkAndCheckInterrupt() {
        LockSupport.park(this);
        return Thread.interrupted();
    }

    /*
     *  各种风味的获取,不同的排他/共享和控制模式。每个都大体相同,但令人讨厌的不同。
     * 只有一点因子分解是可能的,因为异常机制的交互(包括确保我们取消,如果tryAcquire抛出异常)和其他控制,至少不会伤害性能太多。
     */

    /**
     *  以线程已经在队列中的独占不间断模式获取。由条件等待方法使用以及获取。
     */
    final boolean acquireQueued(final Node node, int arg) {
        boolean failed = true;
        try {
            boolean interrupted = false;
            for (;;) {
                final Node p = node.predecessor();//获取当前节点的前驱节点
                //正常情况:前驱节点为首节点而且获取到了前驱节点释放的锁
                if (p == head &&    // 如果前驱为head才有资格抢占锁
                        tryAcquire(arg)) {//尝试获取锁
                    //获取锁成功时,会将当前节点设为首节点
                    setHead(node);//将当前节点设为队列的首节点
                    p.next = null; // help GC
                    failed = false;
                    return interrupted;
                }

                //到这里,说明获取锁失败了
                //如果获取锁失败，则根据节点的waitStatus决定是否需要挂起线程
                if (shouldParkAfterFailedAcquire(p, node) &&//是否要阻塞当前线程
                    parkAndCheckInterrupt())//阻塞当前线程
                    interrupted = true;
            }
        } finally {
            if (failed)
                cancelAcquire(node);
        }
    }

    /**
     *  以独占中断模式获取。
     */
    private void doAcquireInterruptibly(int arg)
        throws InterruptedException {
        final Node node = addWaiter(Node.EXCLUSIVE);
        boolean failed = true;
        try {
            for (;;) {
                final Node p = node.predecessor();
                if (p == head && tryAcquire(arg)) {
                    setHead(node);
                    p.next = null; // help GC
                    failed = false;
                    return;
                }
                if (shouldParkAfterFailedAcquire(p, node) &&
                    parkAndCheckInterrupt())
                    throw new InterruptedException();
            }
        } finally {
            if (failed)
                cancelAcquire(node);
        }
    }

    /**
     *  以独占定时模式获取。
     */
    private boolean doAcquireNanos(int arg, long nanosTimeout)
            throws InterruptedException {
        if (nanosTimeout <= 0L)
            return false;
        final long deadline = System.nanoTime() + nanosTimeout;
        final Node node = addWaiter(Node.EXCLUSIVE);
        boolean failed = true;
        try {
            for (;;) {
                final Node p = node.predecessor();
                if (p == head && tryAcquire(arg)) {
                    setHead(node);
                    p.next = null; // help GC
                    failed = false;
                    return true;
                }
                nanosTimeout = deadline - System.nanoTime();
                if (nanosTimeout <= 0L)
                    return false;
                if (shouldParkAfterFailedAcquire(p, node) &&
                    nanosTimeout > spinForTimeoutThreshold)
                    LockSupport.parkNanos(this, nanosTimeout);
                if (Thread.interrupted())
                    throw new InterruptedException();
            }
        } finally {
            if (failed)
                cancelAcquire(node);
        }
    }

    /**
     * <p>
     *  以共享不间断模式获取。
     */
    private void doAcquireShared(int arg) {
        //将获取锁失败的节点添加到队列
        final Node node = addWaiter(Node.SHARED);
        boolean failed = true;
        try {
            boolean interrupted = false;
            for (;;) {
                //获取前置节点
                final Node p = node.predecessor();
                //如果前置节点是头结点, 那么尝试获取共享锁(由此可以看出,阻塞队列中的节点,只有前置节点为头结点时,才有机会抢占锁)
                if (p == head) {
                    int r = tryAcquireShared(arg);
                    //r>=0说明获取共享锁成功
                    if (r >= 0) {
                        //将当前节点设为头结点,并将节点中包装的线程等属性清空,
                        // 由此可以看出,AQS中的头结点中是没有线程数据的,相当于一个空节点
                        setHeadAndPropagate(node, r);
                        //清空原先头结点的next,帮助GC
                        p.next = null; // help GC
                        //判断是否要中断
                        if (interrupted)
                            selfInterrupt();
                        failed = false;
                        //获取到锁之后才能正常退出死循环
                        return;
                    }
                }
                //到这里说明队列中的节点没有获取到锁,通过死循环阻塞队列中所有未获取到锁的节点
                if (shouldParkAfterFailedAcquire(p, node) &&
                    parkAndCheckInterrupt())
                    interrupted = true;
            }
        } finally {
            if (failed)
                cancelAcquire(node);
        }
    }

    /**
     *  在共享可中断模式下获取。
     */
    private void doAcquireSharedInterruptibly(int arg)
        throws InterruptedException {
        final Node node = addWaiter(Node.SHARED);//1. 将该线程加入阻塞队列中
        boolean failed = true;
        try {
            for (;;) {
                final Node p = node.predecessor();//获取前一个节点
                if (p == head) {
                    int r = tryAcquireShared(arg);//2. 判断state是否为0; r>=0说明为0
                    if (r >= 0) {
                        //把当前节点设置成head节点，并传播 唤醒 所有被await()方法阻塞的节点
                        setHeadAndPropagate(node, r);
                        p.next = null; // help GC
                        failed = false;
                        return;
                    }
                }

                //2. 到此处,说明state != 0
                //检查是否应该阻塞节点
                if (shouldParkAfterFailedAcquire(p, node) &&
                        //阻塞节点
                    parkAndCheckInterrupt())
                    throw new InterruptedException();
            }
        } finally {
            if (failed)
                cancelAcquire(node);
        }
    }

    /**
     *  以共享定时模式获取。
     */
    private boolean doAcquireSharedNanos(int arg, long nanosTimeout)
            throws InterruptedException {
        if (nanosTimeout <= 0L)
            return false;
        final long deadline = System.nanoTime() + nanosTimeout;
        final Node node = addWaiter(Node.SHARED);
        boolean failed = true;
        try {
            for (;;) {
                final Node p = node.predecessor();
                if (p == head) {
                    int r = tryAcquireShared(arg);
                    if (r >= 0) {
                        setHeadAndPropagate(node, r);
                        p.next = null; // help GC
                        failed = false;
                        return true;
                    }
                }
                nanosTimeout = deadline - System.nanoTime();
                if (nanosTimeout <= 0L)
                    return false;
                if (shouldParkAfterFailedAcquire(p, node) &&
                    nanosTimeout > spinForTimeoutThreshold)
                    LockSupport.parkNanos(this, nanosTimeout);
                if (Thread.interrupted())
                    throw new InterruptedException();
            }
        } finally {
            if (failed)
                cancelAcquire(node);
        }
    }

    // Main exported methods

    /**

     *  尝试以独占模式获取。此方法应查询对象的状态是否允许在独占模式下获取该对象的状态,如果是,则查询是否获取该对象的状态。
     * 
     *  <p>此方法总是由执行获取的线程调用。如果此方法报告失败,则获取方法可以对线程排队,如果它还没有排队,直到它被来自某个其他线程的释放发信号通知。
     * 这可以用于实现方法{@link Lock#tryLock()}。
     * 
     * <p>默认实现会抛出{@link UnsupportedOperationException}。
     */
    protected boolean tryAcquire(int arg) {
        throw new UnsupportedOperationException();
    }

    /**
     *  尝试设置状态以反映独占模式下的发布。
     * 
     *  <p>此方法总是由执行释放的线程调用。
     * 
     *  <p>默认实现会抛出{@link UnsupportedOperationException}。

     */
    protected boolean tryRelease(int arg) {
        throw new UnsupportedOperationException();
    }

    /**
     *  尝试在共享模式下获取。此方法应查询对象的状态是否允许在共享模式下获取它,如果是这样,则获取它。
     * 
     *  <p>此方法总是由执行获取的线程调用。如果此方法报告失败,则获取方法可以对线程排队,如果它还没有排队,直到它被来自某个其他线程的释放发信号通知。
     * 
     *  <p>默认实现会抛出{@link UnsupportedOperationException}。
     */
    protected int tryAcquireShared(int arg) {
        throw new UnsupportedOperationException();
    }

    /**
     *  尝试设置状态以反映共享模式下的释放。
     * 
     *  <p>此方法总是由执行释放的线程调用。
     * 
     *  <p>默认实现会抛出{@link UnsupportedOperationException}。
     */
    protected boolean tryReleaseShared(int arg) {
        throw new UnsupportedOperationException();
    }

    /**
     *  如果同步相对于当前(调用)线程排他地保持,则返回{@code true}。每次调用不等待的{@link ConditionObject}方法时,将调用此方法。
     *  (等待方法调用{@link #release}。)。
     * 
     *  <p>默认实现会抛出{@link UnsupportedOperationException}。
     * 此方法仅在{@link ConditionObject}方法内部调用,因此如果不使用条件,则不需要定义。

     */
    protected boolean isHeldExclusively() {
        throw new UnsupportedOperationException();
    }

    /**

     * 以独占模式获取,忽略中断。通过调用至少一次{@link #tryAcquire}实现,返回成功。
     * 否则线程排队,可能反复阻塞和解除阻塞,调用{@link #tryAcquire}直到成功。
     * 此方法可用于实现方法{@link Lock#lock}。
     */
    //lock()失败时执行
    public final void acquire(int arg) {

        if (!tryAcquire(arg) &&//获取锁失败
            acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
            selfInterrupt();
    }

    /**
     *  以独占模式获取,如果中断,则中止。实现通过首先检查中断状态,然后调用至少一次{@link #tryAcquire},成功返回。
     * 否则线程排队,可能重复阻塞和解除阻塞,调用{@link #tryAcquire},直到成功或线程中断。此方法可用于实现方法{@link Lock#lockInterruptibly}。
     */
    public final void acquireInterruptibly(int arg)
            throws InterruptedException {
        if (Thread.interrupted())
            throw new InterruptedException();
        if (!tryAcquire(arg))
            doAcquireInterruptibly(arg);
    }

    /**
     *  尝试以独占模式获取,如果中断则中止,如果超过给定超时,则失败。实现通过首先检查中断状态,然后调用至少一次{@link #tryAcquire},成功返回。
     * 否则,线程排队,可能反复阻塞和解除阻塞,调用{@link #tryAcquire},直到成功或线程中断或超时。
     * 此方法可用于实现方法{@link Lock#tryLock(long,TimeUnit)}。
     */
    public final boolean tryAcquireNanos(int arg, long nanosTimeout)
            throws InterruptedException {
        if (Thread.interrupted())
            throw new InterruptedException();
        return tryAcquire(arg) ||
            doAcquireNanos(arg, nanosTimeout);
    }

    /**
     *  以独占模式发布。如果{@link #tryRelease}返回true,则通过取消屏蔽一个或多个线程来实现。这个方法可以用来实现{@link Lock#unlock}的方法。
     */
    public final boolean release(int arg) {

        if (tryRelease(arg)) {//释放锁(state-1),若释放后锁可被其他线程获取(state=0),返回true

            Node h = head;
            //当前队列不为空且头结点状态不为初始化状态(0)
            if (h != null && h.waitStatus != 0)
                //唤醒同步队列中后续被阻塞的线程
                unparkSuccessor(h);
            return true;
        }
        return false;
    }

    /**
     * Acquires in shared mode, ignoring interrupts.  Implemented by
     * first invoking at least once {@link #tryAcquireShared},
     * returning on success.  Otherwise the thread is queued, possibly
     * repeatedly blocking and unblocking, invoking {@link
     * #tryAcquireShared} until success.
     *
     * <p>
     * 在共享模式下获取,忽略中断。通过首先调用至少一次{@link #tryAcquireShared}实现,返回成功。
     * 否则线程排队,可能反复阻塞和解除阻塞,调用{@link #tryAcquireShared}直到成功。
     * 
     * 
     * @param arg the acquire argument.  This value is conveyed to
     *        {@link #tryAcquireShared} but is otherwise uninterpreted
     *        and can represent anything you like.
     */
    public final void acquireShared(int arg) {
        if (tryAcquireShared(arg) < 0)//获取共享锁失败
            doAcquireShared(arg);
    }

    /**
     * Acquires in shared mode, aborting if interrupted.  Implemented
     * by first checking interrupt status, then invoking at least once
     * {@link #tryAcquireShared}, returning on success.  Otherwise the
     * thread is queued, possibly repeatedly blocking and unblocking,
     * invoking {@link #tryAcquireShared} until success or the thread
     * is interrupted.
     * <p>
     *  在共享模式下获取,如果中断,则中止。通过首先检查中断状态,然后调用至少一次{@link #tryAcquireShared},返回成功。
     * 否则,线程排队,可能反复阻塞和解除阻塞,调用{@link #tryAcquireShared}直到成功或线程中断。
     * 
     * 
     * @param arg the acquire argument.
     * This value is conveyed to {@link #tryAcquireShared} but is
     * otherwise uninterpreted and can represent anything
     * you like.
     * @throws InterruptedException if the current thread is interrupted
     */
    public final void acquireSharedInterruptibly(int arg)
            throws InterruptedException {
        if (Thread.interrupted())
            throw new InterruptedException();
        if (tryAcquireShared(arg) < 0)//计数器state不等于0时,阻塞线程
            doAcquireSharedInterruptibly(arg);
    }

    /**
     * Attempts to acquire in shared mode, aborting if interrupted, and
     * failing if the given timeout elapses.  Implemented by first
     * checking interrupt status, then invoking at least once {@link
     * #tryAcquireShared}, returning on success.  Otherwise, the
     * thread is queued, possibly repeatedly blocking and unblocking,
     * invoking {@link #tryAcquireShared} until success or the thread
     * is interrupted or the timeout elapses.
     *
     * <p>
     *  尝试在共享模式下获取,如果中断,则中止,如果给定的超时时间失败。通过首先检查中断状态,然后调用至少一次{@link #tryAcquireShared},返回成功。
     * 否则,线程排队,可能反复阻塞和解除阻塞,调用{@link #tryAcquireShared}直到成功或线程中断或超时。
     * 
     * 
     * @param arg the acquire argument.  This value is conveyed to
     *        {@link #tryAcquireShared} but is otherwise uninterpreted
     *        and can represent anything you like.
     * @param nanosTimeout the maximum number of nanoseconds to wait
     * @return {@code true} if acquired; {@code false} if timed out
     * @throws InterruptedException if the current thread is interrupted
     */
    public final boolean tryAcquireSharedNanos(int arg, long nanosTimeout)
            throws InterruptedException {
        if (Thread.interrupted())
            throw new InterruptedException();
        return tryAcquireShared(arg) >= 0 ||
            doAcquireSharedNanos(arg, nanosTimeout);
    }

    /**
     * Releases in shared mode.  Implemented by unblocking one or more
     * threads if {@link #tryReleaseShared} returns true.
     *
     * <p>
     *  在共享模式下释放。如果{@link #tryReleaseShared}返回true,则通过解除一个或多个线程来实现。
     * 
     * 
     * @param arg the release argument.  This value is conveyed to
     *        {@link #tryReleaseShared} but is otherwise uninterpreted
     *        and can represent anything you like.
     * @return the value returned from {@link #tryReleaseShared}
     */
    public final boolean releaseShared(int arg) {
        if (tryReleaseShared(arg)) {//state减1成功
            doReleaseShared();
            return true;
        }
        return false;
    }

    // Queue inspection methods

    /**
     * Queries whether any threads are waiting to acquire. Note that
     * because cancellations due to interrupts and timeouts may occur
     * at any time, a {@code true} return does not guarantee that any
     * other thread will ever acquire.
     *
     * <p>In this implementation, this operation returns in
     * constant time.
     *
     * <p>
     *  查询是否有任何线程正在等待获取。注意,因为中断和超时的取消可能在任何时候发生,{@code true}返回不保证任何其他线程将永远获得。
     * 
     *  <p>在此实现中,此操作在常量时间返回。
     * 
     * 
     * @return {@code true} if there may be other threads waiting to acquire
     */
    public final boolean hasQueuedThreads() {
        return head != tail;
    }

    /**
     * Queries whether any threads have ever contended to acquire this
     * synchronizer; that is if an acquire method has ever blocked.
     *
     * <p>In this implementation, this operation returns in
     * constant time.
     *
     * <p>
     * 查询是否有任何线程曾争取获取此同步器;也就是说,如果获取方法被阻止。
     * 
     *  <p>在此实现中,此操作在常量时间返回。
     * 
     * 
     * @return {@code true} if there has ever been contention
     */
    public final boolean hasContended() {
        return head != null;
    }

    /**
     * Returns the first (longest-waiting) thread in the queue, or
     * {@code null} if no threads are currently queued.
     *
     * <p>In this implementation, this operation normally returns in
     * constant time, but may iterate upon contention if other threads are
     * concurrently modifying the queue.
     *
     * <p>
     *  返回队列中的第一个(最长等待)线程,如果当前没有线程排队,则返回{@code null}。
     * 
     *  <p>在此实现中,此操作通常以恒定时间返回,但如果其他线程正在同时修改队列,则可能在争用时重复。
     * 
     * 
     * @return the first (longest-waiting) thread in the queue, or
     *         {@code null} if no threads are currently queued
     */
    public final Thread getFirstQueuedThread() {
        // handle only fast path, else relay
        return (head == tail) ? null : fullGetFirstQueuedThread();
    }

    /**
     * Version of getFirstQueuedThread called when fastpath fails
     * <p>
     *  当fastpath失败时调用getFirstQueuedThread的版本
     * 
     */
    private Thread fullGetFirstQueuedThread() {
        /*
         * The first node is normally head.next. Try to get its
         * thread field, ensuring consistent reads: If thread
         * field is nulled out or s.prev is no longer head, then
         * some other thread(s) concurrently performed setHead in
         * between some of our reads. We try this twice before
         * resorting to traversal.
         * <p>
         *  第一个节点通常是head.next。尝试获取其线程字段,以确保一致的读取：如果线程字段为null或s.prev不再head,那么一些其他线程并发执行setHead在我们的一些读取之间。
         * 我们尝试这两次,然后求助于遍历。
         * 
         */
        Node h, s;
        Thread st;
        if (((h = head) != null && (s = h.next) != null &&
             s.prev == head && (st = s.thread) != null) ||
            ((h = head) != null && (s = h.next) != null &&
             s.prev == head && (st = s.thread) != null))
            return st;

        /*
         * Head's next field might not have been set yet, or may have
         * been unset after setHead. So we must check to see if tail
         * is actually first node. If not, we continue on, safely
         * traversing from tail back to head to find first,
         * guaranteeing termination.
         * <p>
         *  Head的下一个字段可能尚未设置,或者可能在setHead之后未设置。所以我们必须检查是否尾部实际上是第一个节点。如果没有,我们继续,安全地从尾部回到头找到第一,保证终止。
         * 
         */

        Node t = tail;
        Thread firstThread = null;
        while (t != null && t != head) {
            Thread tt = t.thread;
            if (tt != null)
                firstThread = tt;
            t = t.prev;
        }
        return firstThread;
    }

    /**
     * Returns true if the given thread is currently queued.
     *
     * <p>This implementation traverses the queue to determine
     * presence of the given thread.
     *
     * <p>
     *  如果给定的线程当前正在排队,则返回true。
     * 
     *  <p>此实现遍历队列以确定给定线程的存在。
     * 
     * 
     * @param thread the thread
     * @return {@code true} if the given thread is on the queue
     * @throws NullPointerException if the thread is null
     */
    public final boolean isQueued(Thread thread) {
        if (thread == null)
            throw new NullPointerException();
        for (Node p = tail; p != null; p = p.prev)
            if (p.thread == thread)
                return true;
        return false;
    }

    /**
     * 如果明显的第一个排队的线程(如果存在)返回{@code true},则以独占模式等待。
     * 如果此方法返回{@code true},并且当前线程尝试以共享模式获取(即,此方法从{@link #tryAcquireShared}调用),那么它保证当前线程不是第一个排队线。
     * 仅用作ReentrantReadWriteLock中的启发式算法。
     * 
     */
    final boolean apparentlyFirstQueuedIsExclusive() {
        Node h, s;
        /*判断队列中的下一个要唤醒的节点(即头结点的后置节点)是不是在请求写锁,
        如果请求写锁则返回true(返回true表示要阻塞在队列外的要抢占锁的线程)
        从这里可以看出,读锁中的非公平锁是有限制的,即它只能抢占队列中想请求读锁的节点,
        不能抢占请求写锁的节点;这样是为了防止写锁饥饿,无法获取到写锁*/
        return (h = head) != null &&
            (s = h.next)  != null &&
            !s.isShared()         &&
            s.thread != null;
    }

    /**
     * Queries whether any threads have been waiting to acquire longer
     * than the current thread.
     *
     * <p>An invocation of this method is equivalent to (but may be
     * more efficient than):
     *  <pre> {@code
     * getFirstQueuedThread() != Thread.currentThread() &&
     * hasQueuedThreads()}</pre>
     *
     * <p>Note that because cancellations due to interrupts and
     * timeouts may occur at any time, a {@code true} return does not
     * guarantee that some other thread will acquire before the current
     * thread.  Likewise, it is possible for another thread to win a
     * race to enqueue after this method has returned {@code false},
     * due to the queue being empty.
     *
     * <p>This method is designed to be used by a fair synchronizer to
     * avoid <a href="AbstractQueuedSynchronizer#barging">barging</a>.
     * Such a synchronizer's {@link #tryAcquire} method should return
     * {@code false}, and its {@link #tryAcquireShared} method should
     * return a negative value, if this method returns {@code true}
     * (unless this is a reentrant acquire).  For example, the {@code
     * tryAcquire} method for a fair, reentrant, exclusive mode
     * synchronizer might look like this:
     *
     *  <pre> {@code
     * protected boolean tryAcquire(int arg) {
     *   if (isHeldExclusively()) {
     *     // A reentrant acquire; increment hold count
     *     return true;
     *   } else if (hasQueuedPredecessors()) {
     *     return false;
     *   } else {
     *     // try to acquire normally
     *   }
     * }}</pre>
     *
     * <p>
     *  查询任何线程是否已经等待获取时间长于当前线程。
     * 
     *  <p>此方法的调用等效于(但可能比)更有效：<pre> {@code getFirstQueuedThread()！= Thread.currentThread()&& hasQueuedThreads()}
     *  </pre>。
     * 
     *  <p>请注意,因为由于中断和超时而导致的取消可能在任何时间发生,{@code true}返回并不保证其他线程将在当前线程之前获取。
     * 同样,由于队列为空,因此在此方法返回{@code false}之后,另一个线程可能赢得比赛以排队。
     * 
     *  <p>此方法旨在由公平同步器使用,以避免<a href="AbstractQueuedSynchronizer#barging">驳船</a>。
     * 这种同步器的{@link #tryAcquire}方法应该返回{@code false},并且其{@link #tryAcquireShared}方法应返回一个负值,如果此方法返回{@code true}
     * (除非这是可重入获取) 。
     *  <p>此方法旨在由公平同步器使用,以避免<a href="AbstractQueuedSynchronizer#barging">驳船</a>。
     * 例如,用于公平,可重入,独占模式同步器的{@code tryAcquire}方法可能如下所示：。
     * 
     * <pre> {@code protected boolean tryAcquire(int arg){if(isHeldExclusively()){//可重入获取; increment hold count return true; }
     *  else if(hasQueuedPredecessors()){return false; } else {// try to acquired usual}}} </pre>。
     * 
     * 
     * @return {@code true} if there is a queued thread preceding the
     *         current thread, and {@code false} if the current thread
     *         is at the head of the queue or the queue is empty
     * @since 1.7
     */
    public final boolean hasQueuedPredecessors() {
        // The correctness of this depends on head being initialized
        // before tail and on head.next being accurate if the current
        // thread is first in queue.
        Node t = tail; // Read fields in reverse initialization order
        Node h = head;
        Node s;
        return h != t &&
            ((s = h.next) == null || s.thread != Thread.currentThread());
    }


    // Instrumentation and monitoring methods

    /**
     * Returns an estimate of the number of threads waiting to
     * acquire.  The value is only an estimate because the number of
     * threads may change dynamically while this method traverses
     * internal data structures.  This method is designed for use in
     * monitoring system state, not for synchronization
     * control.
     *
     * <p>
     *  返回等待获取的线程数的估计值。该值只是一个估计值,因为线程的数量可能会在此方法遍历内部数据结构时动态更改。此方法设计用于监视系统状态,而不是用于同步控制。
     * 
     * 
     * @return the estimated number of threads waiting to acquire
     */
    public final int getQueueLength() {
        int n = 0;
        for (Node p = tail; p != null; p = p.prev) {
            if (p.thread != null)
                ++n;
        }
        return n;
    }

    /**
     * Returns a collection containing threads that may be waiting to
     * acquire.  Because the actual set of threads may change
     * dynamically while constructing this result, the returned
     * collection is only a best-effort estimate.  The elements of the
     * returned collection are in no particular order.  This method is
     * designed to facilitate construction of subclasses that provide
     * more extensive monitoring facilities.
     *
     * <p>
     *  返回包含可能正在等待获取的线程的集合。因为实际的线程集合可能在构造此结果时动态地改变,所返回的集合仅是最大努力估计。返回的集合的元素没有特定的顺序。该方法被设计为便于构建提供更广泛的监测设施的子类。
     * 
     * 
     * @return the collection of threads
     */
    public final Collection<Thread> getQueuedThreads() {
        ArrayList<Thread> list = new ArrayList<Thread>();
        for (Node p = tail; p != null; p = p.prev) {
            Thread t = p.thread;
            if (t != null)
                list.add(t);
        }
        return list;
    }

    /**
     * Returns a collection containing threads that may be waiting to
     * acquire in exclusive mode. This has the same properties
     * as {@link #getQueuedThreads} except that it only returns
     * those threads waiting due to an exclusive acquire.
     *
     * <p>
     *  返回包含可能正在等待以独占模式获取的线程的集合。这具有与{@link #getQueuedThreads}相同的属性,除了它只返回由于独占获取而等待的线程。
     * 
     * 
     * @return the collection of threads
     */
    public final Collection<Thread> getExclusiveQueuedThreads() {
        ArrayList<Thread> list = new ArrayList<Thread>();
        for (Node p = tail; p != null; p = p.prev) {
            if (!p.isShared()) {
                Thread t = p.thread;
                if (t != null)
                    list.add(t);
            }
        }
        return list;
    }

    /**
     * Returns a collection containing threads that may be waiting to
     * acquire in shared mode. This has the same properties
     * as {@link #getQueuedThreads} except that it only returns
     * those threads waiting due to a shared acquire.
     *
     * <p>
     *  返回包含可能在共享模式下等待获取的线程的集合。这具有与{@link #getQueuedThreads}相同的属性,除了它只返回由于共享获取而等待的线程。
     * 
     * 
     * @return the collection of threads
     */
    public final Collection<Thread> getSharedQueuedThreads() {
        ArrayList<Thread> list = new ArrayList<Thread>();
        for (Node p = tail; p != null; p = p.prev) {
            if (p.isShared()) {
                Thread t = p.thread;
                if (t != null)
                    list.add(t);
            }
        }
        return list;
    }

    /**
     * Returns a string identifying this synchronizer, as well as its state.
     * The state, in brackets, includes the String {@code "State ="}
     * followed by the current value of {@link #getState}, and either
     * {@code "nonempty"} or {@code "empty"} depending on whether the
     * queue is empty.
     *
     * <p>
     * 返回标识此同步器的字符串及其状态。
     * 括号中的状态包括后跟{@link #getState}的当前值的字符串{@code"State ="},以及{@code"非空"}或{@code"空"},具体取决于队列为空。
     * 
     * 
     * @return a string identifying this synchronizer, as well as its state
     */
    public String toString() {
        int s = getState();
        String q  = hasQueuedThreads() ? "non" : "";
        return super.toString() +
            "[State = " + s + ", " + q + "empty queue]";
    }


    // Internal support methods for Conditions

    /**
     * Returns true if a node, always one that was initially placed on
     * a condition queue, is now waiting to reacquire on sync queue.
     * <p>
     *  如果某个节点(始终位于条件队列上的节点)现在正在等待在同步队列上重新获取,则返回true。
     * 
     * 
     * @param node the node
     * @return true if is reacquiring
     */
    final boolean isOnSyncQueue(Node node) {
        if (node.waitStatus == Node.CONDITION || node.prev == null)
            return false;
        if (node.next != null) // If has successor, it must be on queue
            return true;
        /*
         * node.prev can be non-null, but not yet on queue because
         * the CAS to place it on queue can fail. So we have to
         * traverse from tail to make sure it actually made it.  It
         * will always be near the tail in calls to this method, and
         * unless the CAS failed (which is unlikely), it will be
         * there, so we hardly ever traverse much.
         * <p>
         *  node.prev可以是非空的,但尚未在队列上,因为CAS将其放置在队列上可能会失败。所以我们必须从尾部走过,以确保它实际上。
         * 它总是在调用这个方法的尾部附近,并且除非CAS失败(这是不太可能的),它将在那里,所以我们几乎没有横越。
         * 
         */
        return findNodeFromTail(node);
    }

    /**
     * Returns true if node is on sync queue by searching backwards from tail.
     * Called only when needed by isOnSyncQueue.
     * <p>
     *  如果节点在同步队列上,通过从尾部向后搜索,则返回true。仅在需要时由isOnSyncQueue调用。
     * 
     * 
     * @return true if present
     */
    private boolean findNodeFromTail(Node node) {
        Node t = tail;
        for (;;) {
            if (t == node)
                return true;
            if (t == null)
                return false;
            t = t.prev;
        }
    }

    /**
     * Transfers a node from a condition queue onto sync queue.
     * Returns true if successful.
     * <p>
     *  将节点从条件队列传输到同步队列。如果成功,返回true。
     * 
     * 
     * @param node the node
     * @return true if successfully transferred (else the node was
     * cancelled before signal)
     */
    final boolean transferForSignal(Node node) {
        /*
         * If cannot change waitStatus, the node has been cancelled.
         * <p>
         *  如果无法更改waitStatus,则该节点已取消。
         * 
         */
        if (!compareAndSetWaitStatus(node, Node.CONDITION, 0))
            return false;

        /*
         * Splice onto queue and try to set waitStatus of predecessor to
         * indicate that thread is (probably) waiting. If cancelled or
         * attempt to set waitStatus fails, wake up to resync (in which
         * case the waitStatus can be transiently and harmlessly wrong).
         * <p>
         *  接合到队列并尝试设置前导的waitStatus以指示线程(可能)等待。如果取消或尝试设置waitStatus失败,唤醒以重新同步(在这种情况下,waitStatus可能会暂时和无害地错误)。
         * 
         */
        Node p = enq(node);
        int ws = p.waitStatus;
        if (ws > 0 || !compareAndSetWaitStatus(p, ws, Node.SIGNAL))
            LockSupport.unpark(node.thread);
        return true;
    }

    /**
     * Transfers node, if necessary, to sync queue after a cancelled wait.
     * Returns true if thread was cancelled before being signalled.
     *
     * <p>
     *  如果需要,传输节点,在取消等待后同步队列。如果线程在发出信号之前被取消,则返回true。
     * 
     * 
     * @param node the node
     * @return true if cancelled before the node was signalled
     */
    final boolean transferAfterCancelledWait(Node node) {
        if (compareAndSetWaitStatus(node, Node.CONDITION, 0)) {
            enq(node);
            return true;
        }
        /*
         * If we lost out to a signal(), then we can't proceed
         * until it finishes its enq().  Cancelling during an
         * incomplete transfer is both rare and transient, so just
         * spin.
         * <p>
         * 如果我们失去一个signal(),那么我们不能继续,直到它完成它的enq()。在不完全转移期间取消是罕见的和短暂的,所以只是旋转。
         * 
         */
        while (!isOnSyncQueue(node))
            Thread.yield();
        return false;
    }

    /**
     * Invokes release with current state value; returns saved state.
     * Cancels node and throws exception on failure.
     * <p>
     *  使用当前状态值调用释放;返回保存的状态。在失败时取消节点并抛出异常。
     * 
     * 
     * @param node the condition node for this wait
     * @return previous sync state
     */
    final int fullyRelease(Node node) {
        boolean failed = true;
        try {
            int savedState = getState();
            if (release(savedState)) {
                failed = false;
                return savedState;
            } else {
                throw new IllegalMonitorStateException();
            }
        } finally {
            if (failed)
                node.waitStatus = Node.CANCELLED;
        }
    }

    // Instrumentation methods for conditions

    /**
     * Queries whether the given ConditionObject
     * uses this synchronizer as its lock.
     *
     * <p>
     *  查询给定的ConditionObject是否使用此同步器作为其锁。
     * 
     * 
     * @param condition the condition
     * @return {@code true} if owned
     * @throws NullPointerException if the condition is null
     */
    public final boolean owns(ConditionObject condition) {
        return condition.isOwnedBy(this);
    }

    /**
     * Queries whether any threads are waiting on the given condition
     * associated with this synchronizer. Note that because timeouts
     * and interrupts may occur at any time, a {@code true} return
     * does not guarantee that a future {@code signal} will awaken
     * any threads.  This method is designed primarily for use in
     * monitoring of the system state.
     *
     * <p>
     *  查询任何线程是否正在等待与此同步器关联的给定条件。注意,因为超时和中断可能在任何时候发生,{@code true}返回不保证未来的{@code signal}将唤醒任何线程。
     * 该方法主要用于监视系统状态。
     * 
     * 
     * @param condition the condition
     * @return {@code true} if there are any waiting threads
     * @throws IllegalMonitorStateException if exclusive synchronization
     *         is not held
     * @throws IllegalArgumentException if the given condition is
     *         not associated with this synchronizer
     * @throws NullPointerException if the condition is null
     */
    public final boolean hasWaiters(ConditionObject condition) {
        if (!owns(condition))
            throw new IllegalArgumentException("Not owner");
        return condition.hasWaiters();
    }

    /**
     * Returns an estimate of the number of threads waiting on the
     * given condition associated with this synchronizer. Note that
     * because timeouts and interrupts may occur at any time, the
     * estimate serves only as an upper bound on the actual number of
     * waiters.  This method is designed for use in monitoring of the
     * system state, not for synchronization control.
     *
     * <p>
     *  返回等待与此同步器关联的给定条件的线程数的估计。注意,因为超时和中断可以在任何时间发生,所以估计仅用作实际数量的服务者的上限。此方法设计用于监视系统状态,而不是用于同步控制。
     * 
     * 
     * @param condition the condition
     * @return the estimated number of waiting threads
     * @throws IllegalMonitorStateException if exclusive synchronization
     *         is not held
     * @throws IllegalArgumentException if the given condition is
     *         not associated with this synchronizer
     * @throws NullPointerException if the condition is null
     */
    public final int getWaitQueueLength(ConditionObject condition) {
        if (!owns(condition))
            throw new IllegalArgumentException("Not owner");
        return condition.getWaitQueueLength();
    }

    /**
     * Returns a collection containing those threads that may be
     * waiting on the given condition associated with this
     * synchronizer.  Because the actual set of threads may change
     * dynamically while constructing this result, the returned
     * collection is only a best-effort estimate. The elements of the
     * returned collection are in no particular order.
     *
     * <p>
     *  返回包含可能在与此同步器关联的给定条件上等待的线程的集合。因为实际的线程集合可能在构造此结果时动态地改变,所返回的集合仅是最大努力估计。返回的集合的元素没有特定的顺序。
     * 
     * 
     * @param condition the condition
     * @return the collection of threads
     * @throws IllegalMonitorStateException if exclusive synchronization
     *         is not held
     * @throws IllegalArgumentException if the given condition is
     *         not associated with this synchronizer
     * @throws NullPointerException if the condition is null
     */
    public final Collection<Thread> getWaitingThreads(ConditionObject condition) {
        if (!owns(condition))
            throw new IllegalArgumentException("Not owner");
        return condition.getWaitingThreads();
    }

    /**
     * Condition implementation for a {@link
     * AbstractQueuedSynchronizer} serving as the basis of a {@link
     * Lock} implementation.
     *
     * <p>Method documentation for this class describes mechanics,
     * not behavioral specifications from the point of view of Lock
     * and Condition users. Exported versions of this class will in
     * general need to be accompanied by documentation describing
     * condition semantics that rely on those of the associated
     * {@code AbstractQueuedSynchronizer}.
     *
     * <p>This class is Serializable, but all fields are transient,
     * so deserialized conditions have no waiters.
     * <p>
     * {@link AbstractQueuedSynchronizer}的条件实现作为{@link Lock}实现的基础。
     * 
     *  <p>此类的方法文档描述了机械,而不是Lock和Condition用户的行为规范。
     * 这个类的导出版本通常需要伴随着描述依赖于相关联的{@code AbstractQueuedSynchronizer}的条件语义的文档。
     * 
     *  <p>此类是Serializable,但所有字段都是短暂的,因此反序列化条件没有服务器。
     * 
     */
    public class ConditionObject implements Condition, java.io.Serializable {
        private static final long serialVersionUID = 1173984872572414699L;
        /** First node of condition queue. */
        private transient Node firstWaiter;
        /** Last node of condition queue. */
        private transient Node lastWaiter;

        /**
         * Creates a new {@code ConditionObject} instance.
         * <p>
         *  创建一个新的{@code ConditionObject}实例。
         * 
         */
        public ConditionObject() { }

        // Internal methods

        /**
         * Adds a new waiter to wait queue.
         * <p>
         *  添加新的服务员等待队列。
         * 
         * 
         * @return its new wait node
         */
        private Node addConditionWaiter() {
            Node t = lastWaiter;
            // If lastWaiter is cancelled, clean out.
            if (t != null && t.waitStatus != Node.CONDITION) {
                unlinkCancelledWaiters();
                t = lastWaiter;
            }
            Node node = new Node(Thread.currentThread(), Node.CONDITION);
            if (t == null)
                firstWaiter = node;
            else
                t.nextWaiter = node;
            lastWaiter = node;
            return node;
        }

        /**
         * Removes and transfers nodes until hit non-cancelled one or
         * null. Split out from signal in part to encourage compilers
         * to inline the case of no waiters.
         * <p>
         *  删除和传输节点,直到命中未取消一个或null。分离出信号部分,鼓励编译器内联没有服务员的情况。
         * 
         * 
         * @param first (non-null) the first node on condition queue
         */
        private void doSignal(Node first) {
            do {
                if ( (firstWaiter = first.nextWaiter) == null)
                    lastWaiter = null;
                first.nextWaiter = null;
            } while (!transferForSignal(first) &&
                     (first = firstWaiter) != null);
        }

        /**
         * Removes and transfers all nodes.
         * <p>
         *  删除和传输所有节点。
         * 
         * 
         * @param first (non-null) the first node on condition queue
         */
        private void doSignalAll(Node first) {
            lastWaiter = firstWaiter = null;
            do {
                Node next = first.nextWaiter;
                first.nextWaiter = null;
                transferForSignal(first);
                first = next;
            } while (first != null);
        }

        /**
         *  从条件队列中取消链接已取消的服务器节点。仅在握住锁时调用。在条件等待期间发生取消时,
         * 以及在看到lastWaiter已取消时插入新服务员时,将调用此函数。需要此方法以避免在没有信号的情况下的垃圾保留。
         * 因此,即使它可能需要完全遍历,它仅在没有信号的情况下发生超时或取消时才起作用。它遍历所有节点,
         * 而不是在特定目标处停止,以将所有指针解除链接到垃圾节点,而不需要在取消风暴期间多次重新遍历。
         * 
         */
        private void unlinkCancelledWaiters() {
            Node t = firstWaiter;
            Node trail = null;
            while (t != null) {
                Node next = t.nextWaiter;
                if (t.waitStatus != Node.CONDITION) {
                    t.nextWaiter = null;
                    if (trail == null)
                        firstWaiter = next;
                    else
                        trail.nextWaiter = next;
                    if (next == null)
                        lastWaiter = trail;
                }
                else
                    trail = t;
                t = next;
            }
        }

        // public methods

        /**
         * 将等待时间最长的线程(如果存在)从此条件的等待队列移动到拥有锁的等待队列。
         */
        public final void signal() {
            if (!isHeldExclusively())
                throw new IllegalMonitorStateException();
            Node first = firstWaiter;
            if (first != null)
                doSignal(first);
        }

        /**
         *  将所有线程从此条件的等待队列移动到拥有锁的等待队列。
         */
        public final void signalAll() {
            if (!isHeldExclusively())
                throw new IllegalMonitorStateException();
            Node first = firstWaiter;
            if (first != null)
                doSignalAll(first);
        }

        /**
         * Implements uninterruptible condition wait.
         * <ol>
         * <li> Save lock state returned by {@link #getState}.
         * <li> Invoke {@link #release} with saved state as argument,
         *      throwing IllegalMonitorStateException if it fails.
         * <li> Block until signalled.
         * <li> Reacquire by invoking specialized version of
         *      {@link #acquire} with saved state as argument.
         * </ol>
         * <p>
         *  实现不间断条件等待。
         * <ol>
         *  <li>保存由{@link #getState}返回的锁定状态。
         *  <li>以保存的状态作为参数调用{@link #release},如果失败则抛出IllegalMonitorStateException。 <li>在发出信号之前阻止。
         *  <li>以保存的状态作为参数调用{@link #acquire}的专用版本,重新获取。
         * </ol>
         */
        public final void awaitUninterruptibly() {
            Node node = addConditionWaiter();
            int savedState = fullyRelease(node);
            boolean interrupted = false;
            while (!isOnSyncQueue(node)) {
                LockSupport.park(this);
                if (Thread.interrupted())
                    interrupted = true;
            }
            if (acquireQueued(node, savedState) || interrupted)
                selfInterrupt();
        }

        /*
         * For interruptible waits, we need to track whether to throw
         * InterruptedException, if interrupted while blocked on
         * condition, versus reinterrupt current thread, if
         * interrupted while blocked waiting to re-acquire.
         * <p>
         *  对于可中断的等待,我们需要跟踪是否抛出InterruptedException,如果在条件阻塞的情况下中断,与重新中断当前线程,如果在阻塞等待重新获取时中断。
         * 
         */

        /** Mode meaning to reinterrupt on exit from wait */
        private static final int REINTERRUPT =  1;
        /** Mode meaning to throw InterruptedException on exit from wait */
        private static final int THROW_IE    = -1;

        /**
         * Checks for interrupt, returning THROW_IE if interrupted
         * before signalled, REINTERRUPT if after signalled, or
         * 0 if not interrupted.
         * <p>
         *  检查中断,如果在发出信号之前中断,返回THROW_IE,如果发出信号后返回REINTERRUPT,如果未中断则返回0。
         * 
         */
        private int checkInterruptWhileWaiting(Node node) {
            return Thread.interrupted() ?
                (transferAfterCancelledWait(node) ? THROW_IE : REINTERRUPT) :
                0;
        }

        /**
         * Throws InterruptedException, reinterrupts current thread, or
         * does nothing, depending on mode.
         * <p>
         *  抛出InterruptedException,重新中断当前线程,或不做任何事情,具体取决于模式。
         * 
         */
        private void reportInterruptAfterWait(int interruptMode)
            throws InterruptedException {
            if (interruptMode == THROW_IE)
                throw new InterruptedException();
            else if (interruptMode == REINTERRUPT)
                selfInterrupt();
        }

        /**
         * Implements interruptible condition wait.
         * <ol>
         * <li> If current thread is interrupted, throw InterruptedException.
         * <li> Save lock state returned by {@link #getState}.
         * <li> Invoke {@link #release} with saved state as argument,
         *      throwing IllegalMonitorStateException if it fails.
         * <li> Block until signalled or interrupted.
         * <li> Reacquire by invoking specialized version of
         *      {@link #acquire} with saved state as argument.
         * <li> If interrupted while blocked in step 4, throw InterruptedException.
         * </ol>
         * <p>
         *  实现可中断条件等待。
         * <ol>
         *  <li>如果当前线程中断,则抛出InterruptedException。 <li>保存由{@link #getState}返回的锁定状态。
         *  <li>以保存的状态作为参数调用{@link #release},如果失败则抛出IllegalMonitorStateException。 <li>在发出信号或中断前阻止。
         *  <li>以保存的状态作为参数调用{@link #acquire}的专用版本,重新获取。 <li>如果在步骤4中被阻止时中断,请抛出InterruptedException。
         * </ol>
         */
        public final void await() throws InterruptedException {
            if (Thread.interrupted())
                throw new InterruptedException();
            Node node = addConditionWaiter();
            int savedState = fullyRelease(node);
            int interruptMode = 0;
            while (!isOnSyncQueue(node)) {
                LockSupport.park(this);
                if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
                    break;
            }
            if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
                interruptMode = REINTERRUPT;
            if (node.nextWaiter != null) // clean up if cancelled
                unlinkCancelledWaiters();
            if (interruptMode != 0)
                reportInterruptAfterWait(interruptMode);
        }

        /**
         * Implements timed condition wait.
         * <ol>
         * <li> If current thread is interrupted, throw InterruptedException.
         * <li> Save lock state returned by {@link #getState}.
         * <li> Invoke {@link #release} with saved state as argument,
         *      throwing IllegalMonitorStateException if it fails.
         * <li> Block until signalled, interrupted, or timed out.
         * <li> Reacquire by invoking specialized version of
         *      {@link #acquire} with saved state as argument.
         * <li> If interrupted while blocked in step 4, throw InterruptedException.
         * </ol>
         * <p>
         * 实现定时条件等待。
         * <ol>
         *  <li>如果当前线程中断,则抛出InterruptedException。 <li>保存由{@link #getState}返回的锁定状态。
         *  <li>以保存的状态作为参数调用{@link #release},如果失败则抛出IllegalMonitorStateException。 <li>在发出信号,中断或超时之前阻止。
         *  <li>以保存的状态作为参数调用{@link #acquire}的专用版本,重新获取。 <li>如果在步骤4中被阻止时中断,请抛出InterruptedException。
         * </ol>
         */
        public final long awaitNanos(long nanosTimeout)
                throws InterruptedException {
            if (Thread.interrupted())
                throw new InterruptedException();
            Node node = addConditionWaiter();
            int savedState = fullyRelease(node);
            final long deadline = System.nanoTime() + nanosTimeout;
            int interruptMode = 0;
            while (!isOnSyncQueue(node)) {
                if (nanosTimeout <= 0L) {
                    transferAfterCancelledWait(node);
                    break;
                }
                if (nanosTimeout >= spinForTimeoutThreshold)
                    LockSupport.parkNanos(this, nanosTimeout);
                if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
                    break;
                nanosTimeout = deadline - System.nanoTime();
            }
            if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
                interruptMode = REINTERRUPT;
            if (node.nextWaiter != null)
                unlinkCancelledWaiters();
            if (interruptMode != 0)
                reportInterruptAfterWait(interruptMode);
            return deadline - System.nanoTime();
        }

        /**
         * Implements absolute timed condition wait.
         * <ol>
         * <li> If current thread is interrupted, throw InterruptedException.
         * <li> Save lock state returned by {@link #getState}.
         * <li> Invoke {@link #release} with saved state as argument,
         *      throwing IllegalMonitorStateException if it fails.
         * <li> Block until signalled, interrupted, or timed out.
         * <li> Reacquire by invoking specialized version of
         *      {@link #acquire} with saved state as argument.
         * <li> If interrupted while blocked in step 4, throw InterruptedException.
         * <li> If timed out while blocked in step 4, return false, else true.
         * </ol>
         * <p>
         *  实现绝对定时条件等待。
         * <ol>
         *  <li>如果当前线程中断,则抛出InterruptedException。 <li>保存由{@link #getState}返回的锁定状态。
         *  <li>以保存的状态作为参数调用{@link #release},如果失败则抛出IllegalMonitorStateException。 <li>在发出信号,中断或超时之前阻止。
         *  <li>以保存的状态作为参数调用{@link #acquire}的专用版本,重新获取。 <li>如果在步骤4中被阻止时中断,请抛出InterruptedException。
         *  <li>如果在步骤4中被阻止时超时,则返回false,否则为true。
         * </ol>
         */
        public final boolean awaitUntil(Date deadline)
                throws InterruptedException {
            long abstime = deadline.getTime();
            if (Thread.interrupted())
                throw new InterruptedException();
            Node node = addConditionWaiter();
            int savedState = fullyRelease(node);
            boolean timedout = false;
            int interruptMode = 0;
            while (!isOnSyncQueue(node)) {
                if (System.currentTimeMillis() > abstime) {
                    timedout = transferAfterCancelledWait(node);
                    break;
                }
                LockSupport.parkUntil(this, abstime);
                if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
                    break;
            }
            if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
                interruptMode = REINTERRUPT;
            if (node.nextWaiter != null)
                unlinkCancelledWaiters();
            if (interruptMode != 0)
                reportInterruptAfterWait(interruptMode);
            return !timedout;
        }

        /**
         * Implements timed condition wait.
         * <ol>
         * <li> If current thread is interrupted, throw InterruptedException.
         * <li> Save lock state returned by {@link #getState}.
         * <li> Invoke {@link #release} with saved state as argument,
         *      throwing IllegalMonitorStateException if it fails.
         * <li> Block until signalled, interrupted, or timed out.
         * <li> Reacquire by invoking specialized version of
         *      {@link #acquire} with saved state as argument.
         * <li> If interrupted while blocked in step 4, throw InterruptedException.
         * <li> If timed out while blocked in step 4, return false, else true.
         * </ol>
         * <p>
         *  实现定时条件等待。
         * <ol>
         * <li>如果当前线程中断,则抛出InterruptedException。 <li>保存由{@link #getState}返回的锁定状态。
         *  <li>以保存的状态作为参数调用{@link #release},如果失败则抛出IllegalMonitorStateException。 <li>在发出信号,中断或超时之前阻止。
         *  <li>以保存的状态作为参数调用{@link #acquire}的专用版本,重新获取。 <li>如果在步骤4中被阻止时中断,请抛出InterruptedException。
         *  <li>如果在步骤4中被阻止时超时,则返回false,否则为true。
         * </ol>
         */
        public final boolean await(long time, TimeUnit unit)
                throws InterruptedException {
            long nanosTimeout = unit.toNanos(time);
            if (Thread.interrupted())
                throw new InterruptedException();
            Node node = addConditionWaiter();
            int savedState = fullyRelease(node);
            final long deadline = System.nanoTime() + nanosTimeout;
            boolean timedout = false;
            int interruptMode = 0;
            while (!isOnSyncQueue(node)) {
                if (nanosTimeout <= 0L) {
                    timedout = transferAfterCancelledWait(node);
                    break;
                }
                if (nanosTimeout >= spinForTimeoutThreshold)
                    LockSupport.parkNanos(this, nanosTimeout);
                if ((interruptMode = checkInterruptWhileWaiting(node)) != 0)
                    break;
                nanosTimeout = deadline - System.nanoTime();
            }
            if (acquireQueued(node, savedState) && interruptMode != THROW_IE)
                interruptMode = REINTERRUPT;
            if (node.nextWaiter != null)
                unlinkCancelledWaiters();
            if (interruptMode != 0)
                reportInterruptAfterWait(interruptMode);
            return !timedout;
        }

        //  support for instrumentation

        /**
         * Returns true if this condition was created by the given
         * synchronization object.
         *
         * <p>
         *  如果此条件由给定的同步对象创建,则返回true。
         * 
         * 
         * @return {@code true} if owned
         */
        final boolean isOwnedBy(AbstractQueuedSynchronizer sync) {
            return sync == AbstractQueuedSynchronizer.this;
        }

        /**
         * Queries whether any threads are waiting on this condition.
         * Implements {@link AbstractQueuedSynchronizer#hasWaiters(ConditionObject)}.
         *
         * <p>
         *  查询任何线程是否正在等待此条件。实现{@link AbstractQueuedSynchronizer#hasWaiters(ConditionObject)}。
         * 
         * 
         * @return {@code true} if there are any waiting threads
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
         *         returns {@code false}
         */
        protected final boolean hasWaiters() {
            if (!isHeldExclusively())
                throw new IllegalMonitorStateException();
            for (Node w = firstWaiter; w != null; w = w.nextWaiter) {
                if (w.waitStatus == Node.CONDITION)
                    return true;
            }
            return false;
        }

        /**
         * Returns an estimate of the number of threads waiting on
         * this condition.
         * Implements {@link AbstractQueuedSynchronizer#getWaitQueueLength(ConditionObject)}.
         *
         * <p>
         *  返回等待此条件的线程数的估计值。实现{@link AbstractQueuedSynchronizer#getWaitQueueLength(ConditionObject)}。
         * 
         * 
         * @return the estimated number of waiting threads
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
         *         returns {@code false}
         */
        protected final int getWaitQueueLength() {
            if (!isHeldExclusively())
                throw new IllegalMonitorStateException();
            int n = 0;
            for (Node w = firstWaiter; w != null; w = w.nextWaiter) {
                if (w.waitStatus == Node.CONDITION)
                    ++n;
            }
            return n;
        }

        /**
         * Returns a collection containing those threads that may be
         * waiting on this Condition.
         * Implements {@link AbstractQueuedSynchronizer#getWaitingThreads(ConditionObject)}.
         *
         * <p>
         *  返回包含可能在此Condition上等待的线程的集合。实现{@link AbstractQueuedSynchronizer#getWaitingThreads(ConditionObject)}。
         * 
         * 
         * @return the collection of threads
         * @throws IllegalMonitorStateException if {@link #isHeldExclusively}
         *         returns {@code false}
         */
        protected final Collection<Thread> getWaitingThreads() {
            if (!isHeldExclusively())
                throw new IllegalMonitorStateException();
            ArrayList<Thread> list = new ArrayList<Thread>();
            for (Node w = firstWaiter; w != null; w = w.nextWaiter) {
                if (w.waitStatus == Node.CONDITION) {
                    Thread t = w.thread;
                    if (t != null)
                        list.add(t);
                }
            }
            return list;
        }
    }

    /**
     * Setup to support compareAndSet. We need to natively implement
     * this here: For the sake of permitting future enhancements, we
     * cannot explicitly subclass AtomicInteger, which would be
     * efficient and useful otherwise. So, as the lesser of evils, we
     * natively implement using hotspot intrinsics API. And while we
     * are at it, we do the same for other CASable fields (which could
     * otherwise be done with atomic field updaters).
     * <p>
     *  安装程序支持compareAndSet。我们需要在这里本地实现：为了允许将来的增强,我们不能显式地子类化AtomicInteger,否则将是高效和有用的。
     * 因此,作为邪恶的较小者,我们使用热点内在API本地实现。虽然我们在它,我们对其他CASable字段(否则可以用原子场更新器)做同样的。
     * 
     */
    private static final Unsafe unsafe = Unsafe.getUnsafe();
    private static final long stateOffset;
    private static final long headOffset;
    private static final long tailOffset;
    private static final long waitStatusOffset;
    private static final long nextOffset;

    static {
        try {
            stateOffset = unsafe.objectFieldOffset
                (AbstractQueuedSynchronizer.class.getDeclaredField("state"));
            headOffset = unsafe.objectFieldOffset
                (AbstractQueuedSynchronizer.class.getDeclaredField("head"));
            tailOffset = unsafe.objectFieldOffset
                (AbstractQueuedSynchronizer.class.getDeclaredField("tail"));
            waitStatusOffset = unsafe.objectFieldOffset
                (Node.class.getDeclaredField("waitStatus"));
            nextOffset = unsafe.objectFieldOffset
                (Node.class.getDeclaredField("next"));

        } catch (Exception ex) { throw new Error(ex); }
    }

    /**
     * CAS head field. Used only by enq.
     * <p>
     * CAS头字段。仅由enq使用。
     * 
     */
    private final boolean compareAndSetHead(Node update) {
        return unsafe.compareAndSwapObject(this, headOffset, null, update);
    }

    /**
     * CAS tail field. Used only by enq.
     * <p>
     *  CAS尾场。仅由enq使用。
     * 
     */
    private final boolean compareAndSetTail(Node expect, Node update) {
        return unsafe.compareAndSwapObject(this, tailOffset, expect, update);
    }

    /**
     * CAS waitStatus field of a node.
     * <p>
     *  CAS waitStatus字段的一个节点。
     * 
     */
    private static final boolean compareAndSetWaitStatus(Node node,
                                                         int expect,
                                                         int update) {
        return unsafe.compareAndSwapInt(node, waitStatusOffset,
                                        expect, update);
    }

    /**
     * CAS next field of a node.
     * <p>
     *  CAS下一个节点的字段。
     */
    private static final boolean compareAndSetNext(Node node,
                                                   Node expect,
                                                   Node update) {
        return unsafe.compareAndSwapObject(node, nextOffset, expect, update);
    }
}
